package com.xianggu.demo;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 *  模拟10个工人生产3个产品
 */
public class WorkerSimulation {
    public static void main(String[] args) {
        // 创建固定大小为10的线程池
        ExecutorService executor = Executors.newFixedThreadPool(10);
        
        // 提交10个工人任务
        for (int workerId = 1; workerId <= 10; workerId++) {
            final int id = workerId;
            executor.submit(() -> {
                // 每个工人生产3个产品
                for (int productNum = 1; productNum <= 3; productNum++) {
                    String productId = id + "-" + productNum;
                    System.out.println("工人" + id + "生产了产品: " + productId);
                    try {
                        // 模拟生产耗时
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        
        // 关闭线程池
        executor.shutdown();
        
        // 等待所有任务完成
        while (!executor.isTerminated()) {
            // 空转等待
        }
        
        System.out.println("所有工人生产任务完成!");
    }
}